package com.heyqing.prepare.recursive;

/**
 * ClassName:RecursiveMethod
 * Package:com.heyqing.prepare.recursive
 * Description:
 * 递归题解
 *
 * @Date:2024/7/22
 * @Author:Heyqing
 */
public class RecursiveMethod {

    /**
     * 求N的阶乘
     *
     * @param number
     * @return
     */
    public static int calculateNFactorial(int number) {
        if (number == 1) return 1;
        return number * calculateNFactorial(number - 1);
    }

    /**
     * 打印i到j
     *
     * @param i
     * @param j
     */
    public static void printIToJ(int i, int j) {
        if (i == j) return;
        System.out.print(i + "\t");
        printIToJ(i + 1, j);
    }

    /**
     * 数组求和
     *
     * @param targetArray
     * @return
     */
    public static int addArray(int[] targetArray) {
        return sumIter(targetArray, 0, 0);
    }

    /**
     * 斐波那契
     *
     * @param n
     * @return
     */
    public static int fibonacci(int n) {
        if (n == 1 || n == 2) return 1;
        return fibonacci(n - 1) + fibonacci(n - 2);
    }

    /**********************************************private************************************************/
    /**
     * 递归求和
     *
     * @param targetArray
     * @param start
     * @param currentSum
     * @return
     */
    private static int sumIter(int[] targetArray, int start, int currentSum) {
        if (start == targetArray.length) return currentSum;
        return sumIter(targetArray, start + 1, currentSum + targetArray[start]);
    }


}
